package com.fjwt.gz.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.fjwt.gz.core.cache.RedisUtil;
import com.fjwt.gz.core.constants.CS;
import com.fjwt.gz.core.exception.BizException;
import com.fjwt.gz.db.dto.FansUserDTO;
import com.fjwt.gz.db.entity.FansUserEntity;
import com.fjwt.gz.service.mapper.FansUserMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 会员小程序openid/公众号openid表 服务实现类
 * </p>
 *
 * @author shiyonghao
 * @since 2025-04-09
 */
@Slf4j
@Service
public class FansUserService extends ServiceImpl<FansUserMapper, FansUserEntity> {
    @Autowired
    private FansUserMapper fansMsgMapper;


    /**
     * 根据fansid 和 appid 获取用户信息
     *
     * @param type
     * @param fansId
     * @param officialAppId
     * @return
     */
    public FansUserEntity selectFansByFansIdAndAppid(String type, Long fansId, String officialAppId) {
        return fansMsgMapper.selectFansByFansIdAndAppid(type, fansId, officialAppId);
    }

    public FansUserEntity selectUser(Long activityId, Long fansId) {
        if (activityId == null) {
            throw new RuntimeException("活动id不能为空");
        }
        FansUserEntity fansUserEntity = fansMsgMapper.selectUser(activityId, fansId);
        if (fansUserEntity == null) {
            FansUserEntity fansUser = fansMsgMapper.selectAuthUrl(activityId);
            return fansUser;
        }
        return fansUserEntity;
    }

    /**
     * 根据fansid 和 appid 获取用户信息
     *
     * @param type
     * @param appId
     * @return
     */
    public FansUserEntity selectFansByOpenIdAndAppid(String type, String openid, String appId) {
        return fansMsgMapper.selectFansByOpenIdAndAppid(type, openid, appId);
    }

    /**
     * 根据AppId和fansI的查询 OpenId
     *
     * @param appId
     * @param fansId
     * @return
     */
    public FansUserEntity findOpenidByAppIdAndFansId(String appId, Long fansId) {
        return getBaseMapper().findOpenidByAppIdAndFansId(appId, fansId);
    }

    /**
     * 小程序注销
     *
     * @param fansUserDTO
     */
    public void delFanUser(FansUserDTO fansUserDTO, Long currentUserId) {
        Long fansUserId = fansUserDTO.getFansUserId();
        Integer state = fansUserDTO.getState();
        Long fansId = fansUserDTO.getFansId();
        if (ObjectUtil.isEmpty(state) || ObjectUtil.isEmpty(fansUserId) || ObjectUtil.isEmpty(fansId)) {
            throw new BizException("请求参数缺失！");
        }
        log.info("小程序用户注销功能，参数fansUserId：{}, state：{}, fansId:{} , currentUserId：{} ", fansUserId, state, fansId, currentUserId);

        try {
            boolean flag = this.lambdaUpdate()
                    .set(FansUserEntity::getState, state)
                    .set(FansUserEntity::getLogoutSysUserId, currentUserId)
                    .set(FansUserEntity::getLogoutTime, new Date())
                    .eq(FansUserEntity::getFansUserId, fansUserId)
                    .update();

            if (flag) {
                // 清除token
                RedisUtil.del(
                        CS.getCacheKeyToken(CS.SYS_ROLE_TYPE.MCH, fansId, String.valueOf(fansUserId)),
                        CS.getCacheKeyTokenList(CS.SYS_ROLE_TYPE.MCH, fansId)
                );
            }
        }catch (Exception e){
            throw new BizException("注销失败！");
        }
    }
}
